package auth

import (
	"context"

	"kratos-layout/pkg/jwt/claim"

	"github.com/go-kratos/kratos/v2/middleware/auth/jwt"
)

func AccountIDFromContext(ctx context.Context) (int, error) {
	claims, ok := jwt.FromContext(ctx)
	if !ok {
		return 0, ErrUnauthorized
	}
	c, ok := claims.(*claim.CustomClaims)
	if !ok {
		return 0, ErrUnauthorized
	}
	if c.UserId == 0 {
		return 0, ErrUnauthorized
	}
	return c.UserId, nil
}
